package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.FarmHygieneExaminePig;
import com.jiantong.lawEnforcing.service.FarmHygieneExaminePigService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 动物养殖场动物卫生监督检查情况表(生猪)
 * Created by mz03 on 2017/9/29.
 */
@Slf4j
@OpenBean
public class FarmHygieneExaminePigApis {
    @RpcConsumer
    private FarmHygieneExaminePigService farmHygieneExaminePigService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.farmHygieneExaminePig.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public FarmHygieneExaminePig findById(Long id){
        Response<FarmHygieneExaminePig> response = farmHygieneExaminePigService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find farmHygieneExaminePig read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 创建一条信息
     * @param farmHygieneExaminePig
     * @return
     */
    @OpenMethod(
            key = "create.farmHygieneExaminePig",
            paramNames = {"farmHygieneExaminePig"},
            httpMethods = RequestMethod.POST
    )
    public Long create(FarmHygieneExaminePig farmHygieneExaminePig){
        Response<Long> response = farmHygieneExaminePigService.create(farmHygieneExaminePig);
        if(!response.isSuccess()){
            log.error("failed to create farmHygieneExaminePig read by farmHygieneExaminePig = {}, cause : {}", farmHygieneExaminePig, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     * @param farmHygieneExaminePig
     * @return
     */
    @OpenMethod(
            key = "update.farmHygieneExaminePig",
            paramNames = {"farmHygieneExaminePig"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(FarmHygieneExaminePig farmHygieneExaminePig){
        if(farmHygieneExaminePig.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = farmHygieneExaminePigService.update(farmHygieneExaminePig);
        if(!response.isSuccess()){
            log.error("failed to update farmHygieneExaminePig");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(), "failed to update farmHygieneExaminePig");
        }
        return response.getResult();
    }

    /**
     * 根据FarmHygieneExaminePig对象 分页查询
     * @param pageNo
     * @param pageSize
     * @param farmHygieneExaminePig FarmHygieneExaminePig对象
     * @return
     */
    @OpenMethod(
            key = "find.paging.farmHygieneExaminePig",
            paramNames =
                    {"pageNo","pageSize","farmHygieneExaminePig"},
            httpMethods = RequestMethod.GET
    )
    public Paging<FarmHygieneExaminePig> pagingCasesInformation(Integer pageNo, Integer pageSize,FarmHygieneExaminePig farmHygieneExaminePig){
        PageInfo pageInfo = new PageInfo(pageNo,pageSize);
        Response<Paging<FarmHygieneExaminePig>> response = farmHygieneExaminePigService.paging(pageInfo.getOffset(),pageInfo.getLimit(),farmHygieneExaminePig);
        if (!response.isSuccess()) {
            log.error("failed to find paging farmHygieneExaminePig by farmHygieneExaminePig = {}, cause : {}", farmHygieneExaminePig, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

}
